/**
 * 收银机接口
 * Created by Administrator on 2017/5/7 0007.
 */
var RandomCode =  require('../tools/RandomCode');
var DB =  require('../db/db');
var IncreaseId = require('../tools/IncreaseId');
var MD5 = require('../tools/MD5');
var SMS = require('../tools/sms');


exports.vipRegister = vipRegister;
exports.vipSearch = vipSearch;
exports.getShopGoods = getShopGoods;
exports.uploadOrder = uploadOrder;
exports.nonpaymentOrderByUserId = nonpaymentOrderByUserId;
exports.getGoodsByOrderId = getGoodsByOrderId;

/**会员注册*/
function vipRegister(phone,idcard,cb){
    var uid = IncreaseId.increaseId();
    var password = RandomCode.getCode(6);
    if(phone && idcard){
        var sql = "INSERT INTO k_user(id,account,password,phone,idcard) VALUES ('"
            +uid+"','"
            +phone+"','"
            +MD5.md5(password)+"','"
            +phone+"','"
            +idcard+"')";
        console.log(sql);
        DB.query(sql,function(er,list){
            if(!er){
                var message = "恭喜您注册成功！ 您的6位密码是"+password;
                SMS.sendSms(message,phone);
                vipSearch(phone,cb);

            }else{
                cb(null,false);
            }
        });
    }else{
        cb(null,false);
    }
}
/**手机号查询VIP信息*/
function vipSearch(phone,cb){
    if(phone){
        var sql="select id,account,phone,idcard from k_user WHERE phone='"+phone+"'";
        console.log(sql);
        DB.query(sql,function(er,list){
            if(!er && list && list.length>0){
                var user = list[0];
                var sql2="select * from k_address where uid ='"+user.id+"'";
                DB.query(sql2,function(e,l){
                    if(!e){
                        var obj={
                            user:user,
                            address:l
                        };
                        cb(obj,true);
                    }else{
                        cb(null,false);
                    }
                });

            }else{
                cb(null,false);
            }
        });
    }
}
/**获取店铺产品*/
function getShopGoods(shopId,cb){
    if(shopId) {
        var sql = "select g.id,g.goods_name,g.shop_price,g.good_tax_rate,g.goods_number,g.barcode from k_goods_shop as gs LEFT JOIN k_goods g on gs.goods_id=g.id where gs.shop_id= '" + shopId + "'";
        console.log(sql);
        DB.query(sql, function (er, list) {
            if (!er && list && list.length > 0) {
                var obj={
                    goods:list,
                };
                cb(obj, true);
            } else {
                cb(null, false);
            }
        });
    }
}
/**上传已付款订单信息*/
function uploadOrder(order_sn, user_id, consignee, country, province, city, district, address, zipcode, mobile, goods_amount, shipping_fee, insure_fee, money_paid, order_amount, pay_time, goods_tax, warehouse_op_fee, postcard, pay_trade_no, shopId, add_time, discount, goods, cb){
    var order_id = IncreaseId.increaseId();
    console.log("order_id:"+order_id);
    if(order_sn) {
        var sql = "INSERT INTO k_order_info(" +
            "id,order_sn,user_id,consignee,country,province,city,district,address,zipcode,mobile,goods_amount,shipping_fee," +
            "insure_fee,money_paid,order_amount,pay_time,goods_tax,warehouse_op_fee,postcard,pay_trade_no,shop_id,add_time,discount) VALUES ('"
            + order_id + "','"+ order_sn + "','" + user_id + "','" + consignee + "','" + country + "','" + province + "','" + city + "','"
            + district + "','" + address + "','" + zipcode + "','" + mobile + "','" + goods_amount + "','" + shipping_fee + "','"
            + insure_fee + "','" + money_paid + "','" + order_amount + "','" + pay_time + "','" + goods_tax + "','" + warehouse_op_fee + "','"
            + postcard + "','" + pay_trade_no + "','" + shopId + "','" + add_time + "','" + discount + "')";
        console.log(sql);
        DB.query(sql, function (er, list) {
            if (!er) {
                cb(true);
            } else {
                cb(false);
            }
        });
        console.log("goods:"+goods);
        for (var i=0;i<goods.length;i++) {
            var id = IncreaseId.increaseId();
            var sql2 = "INSERT INTO k_order_goods(id,order_id ,goods_id,goods_number) VALUES ('"
                + id + "','" + order_id + "','" + goods[i].goods_id + "','" + goods[i].goods_number + "')";
            console.log("sql2:" + sql2);
            DB.query(sql2, function (er, list) {
                if (!er) {
                    cb(true);
                } else {
                    cb(false);
                }
            });
        }
    }else {
        cb(false);
    }
}
/**通过会员身份证ID查未付款订单列表*/
 function nonpaymentOrderByUserId(idcard,phone,cb) {
        var sql = "SELECT order_sn,consignee,postcard,country,province,city,district,address,zipcode,mobile,goods_amount,shipping_fee,insure_fee,money_paid,order_amount,goods_tax,warehouse_op_fee,add_time,discount FROM k_user as u LEFT JOIN k_order_info as o ON u.id = o.user_id WHERE o.pay_status = 0 and u.idcard = '" + idcard + "' or u.phone ='"+ phone + "'";
        console.log(sql);
        DB.query(sql, function (er, list) {
            if (!er) {
                var obj = {
                    orders:list
                };
                cb(obj, true);
            } else {
                cb(null, false);
            }
        });

}
/**根据订单orderID取商品列表*/
function getGoodsByOrderId(order_id,cb) {
    if(order_id) {
        var sql = "SELECT g.* from  k_goods as g LEFT JOIN k_order_goods as og ON og.goods_id = g.id WHERE og.order_id ='" + order_id + "'";
        console.log(sql);
        DB.query(sql, function (er, list) {
            if (!er) {
                var obj = {
                    goods:list
                };
                cb(obj, true);
            } else {
                cb(null, false);
            }
        });
    }
}

